package cn.csust.net2.manager.shared.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import cn.csust.net2.manager.client.module.ByGrade;
import cn.csust.net2.manager.shared.po.Authority;
import cn.csust.net2.manager.shared.po.Module;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.store.Record;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.google.gwt.i18n.client.DateTimeFormat;

public class Utils {
	/**
	 * 将权限的LIST转化为MAP<MODULE,Map<Module,>,HashMap本身是不能进行排序的，
	 * 会导致每次登录的时候生成的界面都是变动的，采用LinkedHashMap可以进行排序
	 * @param list
	 * @return
	 */
	public static Map<Module, List<Authority>> changeToMap(List<Authority> list) {

		Map<Module, List<Authority>> map = new LinkedHashMap<Module, List<Authority>>();
		for (Authority a : list) {
			List<Authority> l = map.get(a.getModule());
			if (l == null) {
				l = new ArrayList<Authority>();
				l.add(a);
				map.put(a.getModule(), l);
			} else {
				l.add(a);
			}
		}
		return map;

	}
	public static SimpleComboBox<String> createSimpleComboBox(String[] data) {
		SimpleComboBox<String> combo = new SimpleComboBox<String>();
		if (data.length == 0) {
			return combo;
		} else {
			for (String str : data) {
				combo.add(str);
			}
		}
		return combo;
	}

	/**
	 * 将STRING按给出的SYMBOL分开
	 * 
	 */
	public static String[] split(String org, String symbol) {

		return org.split(symbol);

	}

	/**
	 * 去掉STRING左右两端各去掉两个字符串
	 */
	public static String Remove(String org) {

		return org.substring(1, org.length() - 1);

	}

	/**
	 * 去掉字符串的空格
	 */
	public static String trim(String org) {

		return org.trim();
	}

	/**
	 * 根据名称找到
	 * 
	 * @return
	 */
	public static String getOrg(String sys) {
		String org = null;
		if (sys.equals(ByGrade.ADDRESS))
			org = "address";
		else if (sys.equals(ByGrade.BEAN_JI))
			org = "banji";
		else if (sys.equals(ByGrade.BIRTHDAY))
			org = "birthday";
		else if (sys.equals(ByGrade.BIRTHPLACE))
			org = "birthplace";
		else if (sys.equals(ByGrade.CELLPHONE))
			org = "cellPhone";
		else if (sys.equals(ByGrade.DORMITORY))
			org = "dormitory";
		else if (sys.equals(ByGrade.NAME))
			org = "name";
		else if (sys.equals(ByGrade.NATION))
			org = "nation";
		else if (sys.equals(ByGrade.POST_CODE))
			org = "postcode";
		else if (sys.equals(ByGrade.QQ))
			org = "qq";
		else if (sys.equals(ByGrade.SEX))
			org = "sex";
		else if (sys.equals(ByGrade.SPECIALTY))
			org = "specialty";
		else if (sys.equals(ByGrade.STUDENT_NUMBER))
			org = "userID";
		else if (sys.equals(ByGrade.TELLPHONE))
			org = "telephone";
		return org;

	}

	/**
	 * 根据字符串长度生成随即字符串
	 * @param length
	 * @return
	 */
	public static final String randomString(int length) {
		Random randGen = null;
		char[] numbersAndLetters = null;
		Object initLock = new Object();
		if (length < 1) {
			return null;
		}
		if (randGen == null) {
			synchronized (initLock) {
				if (randGen == null) {
					randGen = new Random();
					numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz"
							+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
							.toCharArray();
					// numbersAndLetters =
					// ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
				}
			}
		}
		char[] randBuffer = new char[length];
		for (int i = 0; i < randBuffer.length; i++) {
			randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
			// randBuffer[i] = numbersAndLetters[randGen.nextInt(35)];
		}
		return new String(randBuffer);
	}
	
	/**
	 * 传入一个文件的文件名，返回该文件的后缀，
	 * 如  	"image.jpg", 返回	"jpg"
	 * @param str
	 * @return
	 */
	public static String getSuffix(String str) {
		return str.substring(str.length() - 3);
	}
	
	public static String dateTimeFormat(Date date) {
		return DateTimeFormat.getFormat("yyyy年MM月dd日").format(date);
	}
	public static <T> List<T> turnRecordToT(List<Record> listOrg) {
		List<T> list = new ArrayList<T>();
		for (Record record : listOrg) {
			T t = ((BeanModel) record.getModel()).getBean();
			list.add(t);
		}
		return list;
	}
	public static <T> List<T> turnBeanModelToT(List<BeanModel> listOrg) {
		List<T> list = new ArrayList<T>();
		for (BeanModel record : listOrg) {
			T t = record.getBean();
			list.add(t);
		}
		return list;
	}
	public static String dateFormat(Date date){
		 DateTimeFormat formate = DateTimeFormat.getFormat("yyyy-MM-dd");
		 String dates = formate.format(date);
		 return dates;
	}
//	public static void main(String[] args) throws ParseException {
//		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//		Date  d = df.parse("2009-09-01");
//		System.out.println(getNowTerm(d));
//	}
	
}
